package jjn.carl.monotonic_stack;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/26 23:19
 */
public class LeetCode739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int length = temperatures.length;
        int[] result = new int[length];
        Deque<Integer> deque = new ArrayDeque<>();
        for (int i = 0; i < length; i++) {
            while (!deque.isEmpty() && temperatures[i] > temperatures[deque.peekLast()]) {
                Integer index = deque.pollLast();
                result[index] = i - index;
            }
            deque.offer(i);
        }
        return result;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int[] temperatures = new int[count];
        for (int i = 0; i < count; i++) {
            temperatures[i] = scanner.nextInt();
        }
        int[] dailyTemperatures = new LeetCode739().dailyTemperatures(temperatures);
        System.out.println(Arrays.toString(dailyTemperatures));
    }
}
